# -*- coding:utf-8 -*-
import re
from demo.config import onlyGetiNameFile,getiNameFile,govNameFile,danyifuFile,duoyifuFile,chengduifuFile,spaceFlag,chineseCharacter,numberFile,characterFile,continueNumFlag,continueCharFg,addressFile,emailFile,urlFile,qqFile,phoneFile,telphoneFile,weichatFile,specialNameFile,standardNameflag,proviceNameFile,cityNameFile,countyNameFile,companySuffixFile828,companyMaxLength,companyMinLength
from corpusPickleHandler import readPickle

gdict={u'零':'0',u'壹':'1',u'贰':'2',u'叁':'3',u'肆':'4',u'伍':'5',u'陆':'6',u'柒':'7',u'捌':'8',u'玖':'9'}
ddict={u'一':'1',u'二':'2',u'三':'3',u'四':'4',u'五':'5',u'六':'6',u'七':'7',u'八':'8',u'九':'9'}

danyifu=readPickle(danyifuFile)
duoyifu=readPickle(duoyifuFile)
chengduifu=readPickle(chengduifuFile)
numberfu=readPickle(numberFile)
characterfu=readPickle(characterFile)
# 常用词
addres=readPickle(addressFile)
email=readPickle(emailFile)
url=readPickle(urlFile)
QQ=readPickle(qqFile)
phone=readPickle(phoneFile)
telphone=readPickle(telphoneFile)
weichat=readPickle(weichatFile)
specialName=readPickle(specialNameFile)
# 行政区划信息
provice=readPickle(proviceNameFile)
provice.append(u'集团')
provice.append(u'中国')
provice.append(u'中')

city=readPickle(cityNameFile)
county=readPickle(countyNameFile)
# 事业单位名称
govNameList=readPickle(govNameFile)
# 个体户名称
getiNameList=readPickle(getiNameFile)

getiAdd=[u'站',u'院',u'店',u'部',u'厂']
getiNameList=getiNameList+getiAdd
# 仅仅个体户名称
onlyGetiNameList=readPickle(onlyGetiNameFile)

# 公司名称后缀信息
companySuffix=readPickle(companySuffixFile828)
companySuffix.append(u'企业')
# 将企业公司名称和事业单位名称进行汇总，重新赋值给companySuffix
companySuffix=companySuffix+getiNameList
# qq常用词中容易引起混乱，进行临时替换
qqTempFlag=u'扣：|Ｑ：|Q：|Q:'
addTempFlag=u'Ａ：|A:'
emailTempFlag=u'Ｅ：|邮：|E:'
weichatTempFlag=u'Ｖ：|微：|V:|wechat号'
webTempFlag=u'ｈｔｔｐ：'
telTempFlag=u'Ｔ：|Ｆ：|电：|T:|F:'
phoneTempFlag=u'Ｍ：|M:|手机号码'

companyExcludeCorpus=u'沙井的|占地面|大型的|西北的|前瞻性|致力于|公司响应|出口企业|同属姊妹|建有一座|功能完备|更名为|生产车间|科技园的|今天的|最齐全|建工与|销售处和|二十四小时|前生为|进入|又名|国际会议|设有产品|在泰兴|数一数二|属下有|国家科学|沈阳的|镀锌管的|大千路|有部分|鑫泰广场|始终努力于|精良的|投资的|众多的|均有分|专卖店及|同属一家|现设有|专用线|概括|以用户|石材的|名牌|挂靠|专业的|专业生产|最专业|的几何|创办于|承接|的装饰|在河南|集团母|餐馆的|最快的|现由|大力支持|已与众多|艺术馆和|独立的|配件的|的中信|之后在|售后服务|交界的|驻佛山|旗舰的|的代理|及相应|最环保|各种|高度盛赞|以留各庄|先后与|已发展|大型模具|衷心感谢|认证的|陕南的|企业的|为数不多|保定的|公司在|私营企业|口碑很好|和核电|物资和|行政许可|资质深后|东风东路|最信赖|最高|联合建设|优质价廉|资质的|一体的|领先的|商业广场|最权威|家居广场|美食广场|国际广场|赛德广场|最近的|被审批|产品推荐|改名|中国的|武汉的|一家橱柜|有注册|泗州广场|本土的|所有的|分为|工业区的|供应全球|原为|仓库及|在杭州的|交通要道|环球贸易|自有的|番禺的|的名牌|的用户|铝业的|最具影响|中小企业|一家大型|已重新|系原|两家销售|最正规|注册有|服务于|厂分为|没有|其中的|统一的|保障的|由江苏|各便利店|�|财富广场|湖南的|昆明的|古镇的|防爆的|制造商在|总面积|注册的|设备的|有一三|自主创办|代办|索菲特|代表团|厂与|总部|跨国|电线杆|公司找|址山广场|厂及|购物|报警|销售的|广泛|消费者|全球家居|擎天的|国际先进|开设|米路南|新都会|环城西|望江路|环球广场|批准|武汉及|十大|市场西部|长沙东岸|所旁|火车站|城市商业|家丽路|市场战略|市的|规模|营销及售后|美~|在编的|产品体验|杨北路|由原|信息广场|生产厂家|观音|公司为|顾客|中路|\[|最具潜力|请找|耗资|仪器的|群岛|电缆的|所投资|热忱|原称|全新|全系列|拥有|附近|某|作为|推行|别的|二环东路|奥体中心|规模化|所属|前进路|等|成为|已经|厂的|原南京|许多|输送泵|要求|等等|的中心|重庆代理|多座城市|定点|高水准|各地|刷墙|坚持|组建的|首家|联合创办|认定|大连等|等星级|省级|主板上市|总经理|迎宾路|参股|x|将以|安全监控|\.|较早的|以上市|江州区的|研发应用|前进中|扶持|公司及|自己|首选|免费|#|体检|高架桥|开发的|二环路|人民南路|会议中心|会展中心|网球|罗布林卡|环路|金鸡广场|世纪广场|鑫盛家居|高架桥|无缝钢管|组建|销售包|百源街|膨胀剂|起源于|原东莞|简称|合资组建|与美国|等行业|优化配置|我公司|1|2|3|4|5|6|7|8|9|七里庄建材|采用|绿景西路|永安路|国道|民营|区华东|以北京|产品应用|发展潜力|座落|拥有现代|股票交易所|企业文化|在深圳的|属于|国家级水平|大连的|在河北|大学所属|企业改制|控股的|园区的|在成都|山西的|集团的|沿海经济带|世博园等|花园内|九路|分别建有|原北京市|投资建成|在深圳|素质|直销|最便宜|优秀|已成为|属下的|自己的|制作的|简介|前身|建立|都设有|属下的|最好|分别设有|汇票贴现|授权|以河南|厂家与|坐落于|均设有|设立|档次最高|制品的|各大城市|最大|下属|全国|独资|隶属|下面|需求|成立|从事|全资|地区|周边|公司的|唯一|旗下|全资|依托|颇具|下辖|-|--|没有|个|哪家|价格|销售代理|下设|位于|led商贸|进修|拱墅区|下设|较大|直属|最便宜|高素质|参与投资|地址|以及|早期|组成|多家|子公司|时代广场|门的|原名|地处|本公司|指定|负责|设在|原国有|仟岱中国|三大都市|连锁酒店|良好|知名|包括|专营|第一家|良好|集团性|maywall|金安集团|冷库出租|客户|常年|生产的|/|加工就找|公司和|的公司|最早以|坐落|厂和|比较好|有名|好的家装|著名|已开设|最早|等城市|的分公司|国内外|等地|一下|团队建设|律师协会|\.公司|大中城市|等设有|研究所等|_|分别设有|自己的'
# companyExcludeCorpus=u'前身|设立|特区|最大|下属|全国|独资|隶属|等|下面|合作|需求|成立|从事|全资|专业|地区|周边|大型|公司的|唯一|旗下|全资|依托|颇具|下辖|-|--|没有|个|哪家|价格|工商|销售代理|下设|位于|led商贸|进修|假日|拱墅区|下设|较大|直属|最便宜|高素质|参与投资|地址|以及|早期|在|号|组成|多家|系|子公司|时代广场|门的|原名|地处|本公司|指定|负责|诚信|设在|原国有|仟岱中国|设有|三大都市|连锁酒店|良好|知名|包括|专营|第一家|良好|集团性|maywall|金安集团|冷库出租|客户|品牌|常年|生产的|/|加工就找|公司和|的公司'

zoneExcludeCorpus=u'犍为|无为|即墨|今是|大为'


# 第二步识别
def step2Handler(text):
    qqtext=tempReplaceHandler(text,qqTempFlag,u"QQ")
    addtext=tempReplaceHandler(qqtext,addTempFlag,u"地址")
    emailtext=tempReplaceHandler(addtext,emailTempFlag,u"邮箱")
    weichattext=tempReplaceHandler(emailtext,weichatTempFlag,u"微信")
    webtext=tempReplaceHandler(weichattext,webTempFlag,u"网址")
    teltext=tempReplaceHandler(webtext,telTempFlag,u"座机")
    phonetext=tempReplaceHandler(teltext,phoneTempFlag,u"手机")

    text=chineseCharacterRepalce(phonetext)

    text=danYiFuHandle(text)

    text=duoYiFuHandle(text)

    text=text.replace("\t"," ")
    res=chengduifuHandle(text)

    # res=companyNameAddSpace(res)
    return res

def chineseCharacterRepalce(text):
    listres=re.findall(zoneExcludeCorpus,text)
    if len(listres)>0:
        return text
    return re.sub(chineseCharacter,u" ",text)

def danYiFuHandle(text):
    temp=danyifu['half']+danyifu['full']
    tmp=u'|'.join(set(temp))
    res=re.sub(tmp,u" ",text)
    res=res.replace('/',' ')
    return res

def duoYiFuHandle(text):

    temp=duoyifu['full']
    tmp=u'|'.join(temp)
    return re.sub(tmp,u" ",text)

def chengduifuHandle(text):
    # -------------------------
    text=text.replace("("," 《").replace(")","》").replace("（","《").replace("）","》").replace("［","《").replace("］","》").replace("｛","《").replace("｝","》").replace("「","《").replace("」","》").replace("『","《").replace("』","》").replace("【","《").replace("】","》").replace("<","《").replace(">","》")
    # -------------------------

    flag=['《','》']
    flag=u'|'.join(flag)

    textList=text.split('\n')
    res=[]
    for line in textList:
        if len(line.strip())==0:
            continue
        lineAfter=[]
        lineList=line.split(' ')
        # lineList=re.split(r'[ , ]',line)
        for each in lineList:
            shibiejieguo=re.findall(flag,each)
            # print "--------------------"
            # print each
            # print ','.join(shibiejieguo)
            # print "--------------------"
            if len(shibiejieguo)==0:
                lineAfter.append(each)
            elif len(shibiejieguo)==2 and "《" in each and "》" in each and not each.startswith("《"):
                # print "------------------"
                # print each
                # print "------------------"
                temp=re.split(ur'[《,》]',each)
                # print "----------------"
                # print temp
                # print len(temp)
                # print "----------------"
                if len(temp)==3:
                    # eachT=temp[1]+temp[0]+" "+temp[-1]
                    eachT=temp[1]+temp[0]+temp[-1]
                    # print "------------------"
                    # print eachT
                    # print "------------------"
                    lineAfter.append(eachT)
                else:
                    lineAfter.append(each)
            else:
                lineAfter.append(each)
        line=' '.join(lineAfter)
        res.append(line)
    return '\n'.join(res)
    '''
    temp=chengduifu['full']
    flag=[]
    for each in temp:
        item=each.split(' ')
        flag+=item
    flag=u'|'.join(flag)
    textList=text.split('\n')
    res=[]
    for line in textList:
        if len(line.strip())==0:
            continue
        shibiejieguo=re.findall(flag,line)
        tmp=qianTaoHandler(shibiejieguo,temp,line)

        for each in tmp.split('\n'):
            chengduifuFlag=re.findall(flag,each)
            if isQiantao(chengduifuFlag,temp):
                res.append(each)
            else:
                #需要对非嵌套成对符进行处理，移动处理
                # try:
                    linshi=moveChengduifuContent(temp,chengduifuFlag,each)
                    res.append(linshi)
                # except:
                #     continue
    return '\n'.join(res)
    '''

def moveChengduifuContent(yuanshiFlag,flag,line):
    flagNum=len(flag)
    if flagNum==0:
        return line
    elif (flagNum==2 and isChengdui(flag,yuanshiFlag)):
        flag2=u'|'.join(flag)
        splitRes=re.split(flag2,line)
        res=splitRes[0].split(' ')
        res.insert(-1,u" "+splitRes[1])
        res.append(splitRes[-1])
        tmp=" ".join(res)
        return tmp.strip()
    elif flagNum >=4 and flagNum%2==0:
        return moveManyContent(yuanshiFlag,flag,line)
    else:
        return line

def qianTaoHandler(resList,yuanshiList,line):
    # 对嵌套进行分行处理
    positionList=[]
    position=0
    for item in resList:
        temp=line.find(item,position)
        positionList.append(temp)
        position=temp+1

    fenhangList=[]

    qianTaoPositionList=[]
    flagNum=len(resList)
    flag=0
    if flagNum%2==0:
        for i in xrange(0,flagNum,2):
            temp=resList[i]+u' '+resList[i+1]
            if temp not in yuanshiList:
                flag+=1
                if flag%2==1:
                    tmp=positionList[i]
                    qianTaoPositionList.append(tmp)
                else:
                    tmp=positionList[i+1]+1
                    qianTaoPositionList.append(tmp)
            else:
                continue
    else:
        return line

    sliceNum=len(qianTaoPositionList)
    if sliceNum==0:
        return line
    else:
        for j in xrange(0,sliceNum):
            if j ==0:
                fenhangList.append(line[0:qianTaoPositionList[j]])
            elif j == sliceNum-1:
                fenhangList.append(line[qianTaoPositionList[j]:])
            else:
                fenhangList.append(line[qianTaoPositionList[j-1]:qianTaoPositionList[j]])
        return '\n'.join(fenhangList)

def isChengdui(resList,yuanshiList):
    temp=u" ".join(resList)
    for item in yuanshiList:
        if item==temp:
            return True
    return False

def isQiantao(resList,yuanshiList):
    flagNum=len(resList)
    if flagNum!=0 and flagNum%2==0  :
        for i in xrange(0,flagNum,2):
            temp=resList[i]+u' '+resList[i+1]
            if temp not in yuanshiList:
                return True
    else:
        return False

def moveManyContent(yuanshiList,flagList,line):
    flagNum=len(flagList)
    for i in xrange(0,flagNum,2):
        temp=flagList[i]+u' '+flagList[i+1]
        if temp not in yuanshiList:
            return line
    #此处表示都是普通成对符
    flagPositon=[]
    position=0
    for item in flagList:
        temp=line.find(item,position)
        flagPositon.append(temp)
        position=temp+1

    #对成对符进行切片
    positionLength=len(flagPositon)


    tmp=range(0,positionLength,2)
    # print "++++++++++++++++++++++++++"
    # print flagPositon
    # print tmp
    # print "++++++++++++++++++++++++++"
    tmp.reverse()
    # print "++++++++++++++++++++++++++"
    # print tmp
    # print "++++++++++++++++++++++++++"
    if flagPositon[0]==0:
        pass
    else:
        houduan=None
        context=None
        for i in tmp:
            if i ==tmp[-1]:
                qianduanTemp=line[0:flagPositon[i]]
                if u' ' not in qianduanTemp:
                    context=context
                    houduan=qianduanTemp+houduan
                    return context+houduan
                else:
                    res=qianduanTemp.split(' ')
                    res.insert(-1,u" "+context)
                    res.append(houduan)
                    result=u" ".join(res)
                    return result.strip()
            elif i==tmp[0]:
                j=i-2
                houduanTemp=line[flagPositon[i+1]+1:]
                contextTemp=line[flagPositon[i]+1:flagPositon[i+1]]
                qianduanTemp=line[flagPositon[j+1]+1:flagPositon[i]]

                if u' ' not in qianduanTemp:
                    houduan=qianduanTemp+houduanTemp
                    context=line[flagPositon[j]+1:flagPositon[j+1]]+contextTemp
                else:
                    res=qianduanTemp.split(' ')
                    res.insert(-1,u" "+contextTemp)
                    res.append(houduanTemp)
                    houduan=u" ".join(res)
                    context=line[flagPositon[j]+1:flagPositon[j+1]]
            else:
                j=i-2
                qianduanTemp=line[flagPositon[j+1]+1:flagPositon[i]]
                contextTemp=line[flagPositon[j]+1:flagPositon[j+1]]
                if u' ' not in qianduanTemp:
                    context=contextTemp+context
                    houduan=qianduanTemp+houduan
                else:
                    res=qianduanTemp.split(' ')
                    res.insert(-1,u" "+context)
                    res.append(houduan)
                    houduan=u" ".join(res)
                    context=contextTemp


# 第三步识别
# 总体操作
def step3Handler(text):
    # 1-3-0
    temp=spaceStepOneHandler(text)
    # 1-3-1
    tmp=u""
    for each in temp.split(" "):
        tmp=numSpaceHandler(tmp,each)
    text=tmp.strip()
    #1-3-2
    text=replacingHandler(text)
    #1-3-3
    # -----------------------------------
    # =======================================
    # 添加新代码
    number=numberfu['half']
    character=characterfu['half']
    halfDuoyifu=duoyifu['half']

    zuhe4=number+character+halfDuoyifu
    tmp4=''.join(zuhe4)
    flag4=r'(['+tmp4+']{5,})'
    #>3
    zuhe31=number+halfDuoyifu
    tmp31=''.join(zuhe31)
    flag31=r'(['+tmp31+']{4,})'
    zuhe32=character+halfDuoyifu
    tmp32=''.join(zuhe32)
    flag32=r'(['+tmp32+']{4,})'

    text=continueHandler4(text,flag4,flag31,flag32,continueNumFlag,continueCharFg)

    # =======================================
    #1-3-4
    result=spaceStepOneHandler(text)
    return result
# 1-3-4替换为1-3-0,同步进行
def spaceStepOneHandler(text):
    textList=re.split(spaceFlag,text)
    result=[]

    # 处理个体户中添加空格流程
    target=u'|'.join(onlyGetiNameList)

    for item in textList:
        itemlen=len(item)
        if itemlen==0:
            continue
        else:
            targetList=re.findall(target,item)
            if len(targetList)==0:
                result.append(item)
            else:
                for onegeti in onlyGetiNameList:

                    if onegeti in item:
                        if item.endswith(onegeti):
                            result.append(item)
                            break
                        else:
                            onegetiIndex=item.rfind(onegeti)
                            temp=itemlen-onegetiIndex-len(onegeti)

                            if onegetiIndex >0 and temp <=5:
                                start=item[0:onegetiIndex]
                                middle=onegeti+" "
                                end=item[onegetiIndex+len(onegeti):]
                                newitem=start+middle+end

                                result.append(newitem)
                            else:
                                result.append(item)

                    else:
                        continue
    return u' '.join(result)
# 1-3-1
def numSpaceHandler(str1,str2):
    arabflag=('1','2','3','4','5','6','7','8','9','0')
    chineseflag=tuple(gdict.keys()+ddict.keys())

    temp11=str1.endswith(arabflag)
    temp12=str2.startswith(chineseflag)
    res1=temp11 & temp12

    temp21=str1.endswith(chineseflag)
    temp22=str2.startswith(arabflag)
    res2=temp21 & temp22

    res= res1 | res2
    if res:
        return str1+str2
    else:
        return str1+u" "+str2
# 1-3-2
def replacingHandler(string):
    res=""
    for item in string:
        item1=strQ2B(item)    #全角转半角
        if item1 in gdict:
            res+=gdict[item1]     #中文数字替换
        elif item1 in ddict:
            res+=ddict[item1]     #汉字数据替换
        else:
            res+=item1
    # print res
    res=res.lower()   #对英文大写进行处理
    return res
def strQ2B(uchar):
    # 全角-半角、大写-小写
    inside_code=ord(uchar)
    # print inside_code
    if inside_code == 12288 or inside_code == 160: #全角空格直接转换
        inside_code = 32
    elif (inside_code >= 65281 and inside_code <= 65374): #全角字符（除空格）根据关系转化
        inside_code -= 65248
    temp=unichr(inside_code)
    return temp
# 1-3-3 1、2、3

def continueHandler4(text,flag4,flag31,flag32,flag21,flag22):
    '''
    :param line:一行一行处理
    :param flag:
    :return:
    '''
    result=[]
    for line in text.split('\n'):
        linshi=[]
        for each in re.split(' |	',line):
            res=re.findall(flag4,each)
            # if len(res)==0 or len(res)==1:
            if len(res)==0:
                zhongjian=continueHandler31(each,flag31,flag32,flag21,flag22)
                linshi.append(zhongjian)
            else:
                guodu=u""
                tmp=each
                for item in res:
                    temp=re.split(item,tmp)
                    zhongjian=continueHandler31(temp[0],flag31,flag32,flag21,flag22)
                    # -----------------------
                    if item.endswith("."):
                        linshi1=zhongjian+u" "+item
                    else:
                        linshi1=zhongjian+u" "+item+u" "
                    # -----------------------
                    # linshi1=zhongjian+u" "+item+u" "
                    tmp=temp[-1]
                    # if temp[0]==u'' and len(temp)==1:
                    if (temp[0]==u'' or len(temp)==1) or len(res)==1:
                        guodu=guodu+linshi1+temp[-1]
                    else:
                        guodu=guodu+linshi1
                linshi.append(guodu)
        eachLine=u' '.join(linshi)
        result.append(eachLine.strip())
    return '\n'.join(result)


def continueHandler31(text,flag31,flag32,flag21,flag22):
    '''
    :param line:一行一行处理
    :param flag:
    :return:
    '''
    result=[]
    for line in text.split('\n'):
        linshi=[]
        for each in re.split(' |	',line):
            res=re.findall(flag31,each)
            if len(res)==0:
                linshi.append(each)
            else:
                guodu=u""
                tmp=each
                for item in res:
                    temp=re.split(item,tmp)
                    zhongjian=continueHandler32(temp[0],flag32,flag21,flag22)
                    if item.endswith("."):
                        linshi1=zhongjian+u" "+item
                    else:
                        linshi1=zhongjian+u" "+item+u" "
                    if (temp[0]==u'' or len(temp)==1) or len(res)==1:
                        guodu=guodu+linshi1+temp[-1]
                    else:
                        guodu=guodu+linshi1
                    tmp=temp[-1]
                    # linshi1=zhongjian+u" "+item+u" "
                    # tmp=temp[-1]
                    # if temp[0]==u'':
                    #     guodu=guodu+linshi1+temp[-1]
                    # else:
                    #     guodu=guodu+linshi1
                linshi.append(guodu)
        eachLine=u' '.join(linshi)
        result.append(eachLine.strip())
    return '\n'.join(result)
def continueHandler32(text,flag32,flag21,flag22):

    '''
    :param line:一行一行处理
    :param flag:
    :return:
    '''
    result=[]
    for line in text.split('\n'):
        linshi=[]
        for each in line.split(' '):
            res=re.findall(flag32,each)
            if len(res)==0:
                linshi.append(each)
            else:
                guodu=u""
                tmp=each
                for item in res:
                    temp=re.split(item,tmp)
                    zhongjian=continueHandler2(temp[0],flag21)
                    zhongjian=continueHandler2(zhongjian,flag22)
                    if item.endswith("."):
                        linshi1=zhongjian+u" "+item
                    else:
                        linshi1=zhongjian+u" "+item+u" "
                    if (temp[0]==u'' or len(temp)==1) or len(res)==1:
                        guodu=guodu+linshi1+temp[-1]
                    else:
                        guodu=guodu+linshi1
                    tmp=temp[-1]

                    # linshi1=zhognjian+u" "+item+u" "
                    # tmp=temp[-1]
                    # if temp[0]==u'':
                    #     guodu=guodu+linshi1+temp[-1]
                    # else:
                    #     guodu=guodu+linshi1
                linshi.append(guodu)
        eachLine=u' '.join(linshi)
        result.append(eachLine.strip())
    return '\n'.join(result)

def continueHandler2(text,flag):
    '''
    :param line:一行一行处理
    :param flag:
    :return:
    '''
    result=[]
    for line in text.split('\n'):
        linshi=[]
        for each in line.split(' '):
            res=re.findall(flag,each)
            if len(res)==0:
                linshi.append(each)
            else:
                for item in res:
                    temp=re.split(item,each)
                    each=temp[0]+u" "+item+u" "+temp[-1]
                linshi.append(each)
        eachLine=u' '.join(linshi)
        result.append(eachLine.strip())
    return '\n'.join(result)

# 第四步处理
def step4Handler(text):
    #address
    addresFlag=u"|".join(addres)
    text=commonWordReplace(text,addresFlag,u"地址")
    #email
    emailFlag=u"|".join(email)
    text=commonWordReplace(text,emailFlag,u"邮箱")

    #url
    urlFlag=u"|".join(url)
    text=commonWordReplace(text,urlFlag,u"网址")

    #qq
    qqFlag=u"|".join(QQ)
    text=commonWordReplace(text,qqFlag,u"QQ")

    #phone
    phoneFlag=u"|".join(phone)
    text=commonWordReplace(text,phoneFlag,u"手机")

    #telphone
    telphoneFlag=u"|".join(telphone)
    text=commonWordReplace(text,telphoneFlag,u"座机")

    #weichat
    weichatFlag=u"|".join(weichat)
    text=commonWordReplace(text,weichatFlag,u"微信")

    #specialName
    specialNameFlag=u"|".join(specialName)
    text=commonWordReplace(text,specialNameFlag,u"特例名")
    #1-4-2
    text=standardNameSpaceHandler(text)

    #1-4-3
    res=spaceStepOneHandler(text)

    return res


# 1-4-1常用词替换:地址、邮箱、网址、QQ、手机、座机、微信、特例名、企业名称
def commonWordReplace(text,targetFlag,replaceObj):
    temp=re.sub(targetFlag,replaceObj,text)
    if replaceObj==u"QQ":
        return commonWordQQAux(temp)
    else:
        return temp

def commonWordQQAux(text):
    text=re.sub("@QQ","@qq",text)
    return re.sub("@vip.QQ","@vip.qq",text)

#1-4-2处理标准输出名称，在同一行中有连续标准名称时进行空格添加操作
def standardNameSpaceHandler(text):
    result=[]
    for line in text.split("\n"):
        linshi=[]
        for each in line.split(" "):
            temp=standardNameSpaceHandlerAux(each,standardNameflag)
            linshi.append(temp)
        tmp=u' '.join(linshi)
        result.append(tmp)
    return '\n'.join(result)
def standardNameSpaceHandlerAux(text,flag):
    temp=re.findall(flag,text)
    tempLength=len(temp)
    if tempLength<=1:
        return text
    else:
        result=u""
        i=1
        for item in temp:
            if i==1:
                tmp=re.split(item,text,1)
                res=tmp[0]+item
                result+=res
                text=tmp[-1]
                i+=1
            else:
                tmp=re.split(item,text,1)
                res=tmp[0]+u" "+item
                result+=res
                text=tmp[-1]
                if i==tempLength:
                    result+=text
                i+=1
        return result
# 1-4-3 多空格处理 使用1-3-4

# 第五步处理
def step5Handler(text):
    #1-5-1
    text=replaceSpaceHandler(text)
    #1-5-2
    text=blankLineHandler(text)
    return text
#1-5-1
def replaceSpaceHandler(text):
    return re.sub('"| |\\t',"\n",text)
#1-5-2
def blankLineHandler(text):
    temp=re.split("\n",text)
    res=[]
    for line in temp:
        line=line.strip()
        if len(line)==0:
            continue
        else:
            res.append(line)
    return '\n'.join(res)
# 企业名称
# 2-2-1
def stepCompanyNameHandler(line):
    line=line.replace('0',u"零").replace('1',u"一").replace('2',u"二").replace('3',u"三").replace('4',u"四").replace('5',u"五").replace('6',u"六").replace('7',u"七").replace('8',u"八").replace('9',u"九")

    temp=companyNameExclude(line,companyExcludeCorpus)

    if not temp:
        # print "---------------------------"
        # print line
        # print "---------------------------"
        return False
    prefix=isIncludeProviceOrCityOrConty(provice,city,county,line)

    if prefix == False:
        return False

    prefixLength=prefix[1]
    # prefixLength=prefix[-1]

    suffix=isIncludeCompanySuffix(companySuffix,line)
    if suffix==False:
        # print "====================="
        # print "ceshi"
        # print "====================="

        return False
    suffixLength=suffix[1]
    # suffixLength=suffix[-1]

    res=isAccordCompanyLength(line,prefixLength,suffixLength)
    return res

def stepGovNameHandler(line):
    line=line.replace('0',u"零").replace('1',u"一").replace('2',u"二").replace('3',u"三").replace('4',u"四").replace('5',u"五").replace('6',u"六").replace('7',u"七").replace('8',u"八").replace('9',u"九")
    # temp=companyNameExclude(line,companyExcludeCorpus)
    # if not temp:
    #     return False
    prefix=isIncludeProviceOrCityOrConty(provice,city,county,line)

    if prefix == False:
        if not line.startswith(u'中国'):
            return False

    govSuffix=isIncludeCompanySuffix(govNameList,line)
    if govSuffix==False:
        return False
    else:
        return True


def stepGetiNameHandler(line):
    line=line.replace('0',u"零").replace('1',u"一").replace('2',u"二").replace('3',u"三").replace('4',u"四").replace('5',u"五").replace('6',u"六").replace('7',u"七").replace('8',u"八").replace('9',u"九")
    # temp=companyNameExclude(line,companyExcludeCorpus)
    # if not temp:
    #     return False
    prefix=isIncludeProviceOrCityOrConty(provice,city,county,line)

    if prefix == False:
        return False

    getiSuffix=isIncludeCompanySuffix(getiNameList,line)
    if getiSuffix==False:
        return False
    else:
        return True

# 1
def isIncludeProviceOrCityOrConty(provice,city,county,line):
    targetProvice=u'|'.join(provice)
    targetCity=u'|'.join(city)
    targetCounty=u'|'.join(county)
    proviceRes=isIncludeTarget(targetProvice,line,'start')
    if proviceRes!=False:
        return proviceRes
    cityRes=isIncludeTarget(targetCity,line,'start')
    if cityRes!=False:
        return cityRes
    countyRes=isIncludeTarget(targetCounty,line,'start')
    if countyRes!=False:
        return countyRes
    else:
        return False
# 2
def isIncludeCompanySuffix(companySuffix,line):
    targetSuffix=u'|'.join(companySuffix)
    return isIncludeTarget(targetSuffix,line,'end')


def isIncludeProviceOrCityOrContyAdd(provice,city,county,line):
    targetProvice=u'|'.join(provice)
    targetCity=u'|'.join(city)
    targetCounty=u'|'.join(county)
    proviceRes=isIncludeTargetAdd(targetProvice,line)
    if proviceRes!=False:
        return proviceRes
    cityRes=isIncludeTargetAdd(targetCity,line)
    if cityRes!=False:
        return cityRes
    countyRes=isIncludeTargetAdd(targetCounty,line)
    if countyRes!=False:
        return countyRes
    else:
        return False

def isIncludeCompanySuffixAdd(companySuffix,line):
    targetSuffix=u'|'.join(companySuffix)
    return isIncludeTargetAdd(targetSuffix,line)

def isIncludeTargetAdd(target,line):
    resList=re.findall(target,line)
    if len(resList)==0:
        return False
    else:
        for item in resList:
            if item in line:
                return True,len(item),resList
            else:
                continue
        return False


# 3
def isAccordCompanyLength(line,prefixLength,suffixLength):
    length=len(line.strip())
    target=length-prefixLength-suffixLength
    if target <= companyMaxLength and target >= companyMinLength:
        return True
    else:
        return False

# 是否包含省、市、县区、公司后缀目标
def isIncludeTarget(target,line,direction):
    resList=re.findall(target,line)
    if len(resList)==0:
        return False
    else:
        if direction=='start':
            for item in resList:
                if line.startswith(item):
                    return True,len(item),resList
                else:
                    continue
            return False
        else:
            for item in resList:
                if line.endswith(item):
                    return True,len(item),resList
                else:
                    continue
            return False

# 临时添加，在开头将qq模块中容易出问题的几个符号替换成标准名称
def tempReplaceHandler(text,flag,target):
    return re.sub(flag,target,text)

def companyNameExclude(text,target):
    targetList=re.findall(target,text)
    # print "-------------------"
    # print text
    # print "|".join(targetList)
    # print "-------------------"
    if len(targetList)==0:
        return True
    else:
        return False


def companyNameAddSpace(text):
    # print "======================"
    # print text
    res=[]
    rowList=text.split('\n')
    for eachrow in rowList:
        if len(eachrow.strip())==0:
            continue
        else:

            eachrowList=re.split(r'[，,。, ]',eachrow)

            for roweach in eachrowList:
                # print "------------------------"
                # print roweach
                # print "------------------------"
                suffixtemp=isIncludeCompanySuffixAdd(companySuffix,roweach)

                if not suffixtemp:
                    # print "------------------------"
                    # print roweach
                    # print suffixtemp
                    # print "------------------------"
                    res.append(roweach)
                else:
                    suffixTarget=suffixtemp[-1]
                    # print "------------------------"
                    # print roweach
                    # print "|".join(suffixTarget)
                    # print "------------------------"
                    if len(suffixTarget)==1:
                        suffixIndex=roweach.rfind(suffixTarget[0])
                        if suffixIndex>0:
                            suffixstart=roweach[:suffixIndex]

                            prefixtemp=isIncludeProviceOrCityOrContyAdd(provice,city,county,suffixstart)
                            if not prefixtemp:
                                res.append(roweach)
                            else:
                                suffixmiddle=suffixTarget[0]+"\n"
                                suffixend=roweach[suffixIndex+len(suffixTarget[0]):]

                                prefixTarget=prefixtemp[-1]
                                prefixIndex=suffixstart.find(prefixTarget[0])
                                prefixstart=suffixstart[0:prefixIndex]+"\n"
                                prefixend=suffixstart[prefixIndex:]

                                newrow=prefixstart+prefixend+suffixmiddle+suffixend
                                res.append(newrow)
                    else:
                        maxIndex=0
                        item=u""
                        for eachsuffix in suffixTarget:
                            Index=roweach.rfind(eachsuffix)
                            if Index>maxIndex:
                                maxIndex=Index
                                item=eachsuffix
                            else:
                                continue
                        suffixstart=roweach[:maxIndex]

                        prefixtemp=isIncludeProviceOrCityOrContyAdd(provice,city,county,suffixstart)
                        if not prefixtemp:
                            res.append(roweach)
                        else:
                            suffixmiddle=item+"\n"
                            suffixend=roweach[maxIndex+len(item):]

                            prefixTarget=prefixtemp[-1]
                            prefixIndex=suffixstart.find(prefixTarget[0])
                            prefixstart=suffixstart[0:prefixIndex]+"\n"
                            prefixend=suffixstart[prefixIndex:]

                            newrow=prefixstart+prefixend+suffixmiddle+suffixend
                            res.append(newrow)

    # print "\n".join(res)
    # print "======================="
    return "\n".join(res)
